package FileCopier;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class FileCopier {
    public static void main(String[] args) {
        // 指定源目录和目标目录
        Path sourceDirectory = Paths.get("D:/code/java/project1/src");
        Path targetDirectory = Paths.get("D:/code/java/project57/src/main/target");

        try (Stream<Path> stream = Files.walk(sourceDirectory)) {
            // 过滤出.java文件
            stream.filter(path -> path.toString().endsWith(".java"))
                    .forEach(path -> copyFile(path, targetDirectory));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void copyFile(Path sourcePath, Path targetDirectory) {
        Path targetPath = targetDirectory.resolve(sourcePath.getFileName());
        try {
            Files.copy(sourcePath, targetPath);
            System.out.println("Copied file: " + sourcePath.getFileName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
